﻿@page "/settings/security"
@using AliasVault.Client.Main.Pages.Settings.Security.Components
@inherits MainBase
@using Microsoft.Extensions.Localization

<LayoutPageTitle>@Localizer["PageTitle"]</LayoutPageTitle>

<PageHeader
    BreadcrumbItems="@BreadcrumbItems"
    Title="@Localizer["PageTitle"]"
    Description="@Localizer["PageDescription"]">
    <CustomActions>
        <RefreshButton OnClick="LoadData" ButtonText="@Localizer["RefreshButton"]" />
    </CustomActions>
</PageHeader>

<PasswordChangeSection OnStatusChanged="LoadData" />
<TwoFactorAuthenticationSection @ref="TwoFactorSection" OnStatusChanged="LoadData" />
<QuickVaultUnlockSection @ref="QuickVaultUnlockSection" OnStatusChanged="LoadData" />
<ActiveSessionsSection @ref="SessionsSection" OnSessionsChanged="LoadData" />
<RecentAuthLogsSection @ref="RecentAuthLogsSection" OnSessionsChanged="LoadData" />
<DeleteAccountSection />

@code {
    private IStringLocalizer Localizer => LocalizerFactory.Create("Pages.Main.Settings.Security.Security", "AliasVault.Client");
    private TwoFactorAuthenticationSection? TwoFactorSection;
    private QuickVaultUnlockSection? QuickVaultUnlockSection;
    private ActiveSessionsSection? SessionsSection;
    private RecentAuthLogsSection? RecentAuthLogsSection;

    /// <inheritdoc />
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        BreadcrumbItems.Add(new BreadcrumbItem { DisplayName = Localizer["BreadcrumbTitle"] });
    }

    /// <inheritdoc />
    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);

        if (firstRender)
        {
            await LoadData();
        }
    }

    /// <summary>
    /// Loads data for both the Two-Factor Authentication and Active Sessions sections concurrently.
    /// </summary>
    /// <returns>A task representing the asynchronous operation.</returns>
    private async Task LoadData()
    {
        await Task.WhenAll(
            TwoFactorSection!.LoadData(),
            QuickVaultUnlockSection!.LoadData(),
            SessionsSection!.LoadData(),
            RecentAuthLogsSection!.LoadData()
        );
    }
}
